package com.alan.crpx;

/**
 * 二分插入排序
 */
public class Efcrpx {
    public static void main(String[] args) {
        int[] a={49,38,65,97,176,213,227,49,78,34,12,164,11,18,1};
        System.out.println("排序之前：");
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+" ");
        }
        System.out.println();
        sort(a);
        System.out.println("排序之后：");
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+" ");
        }
    }

    public static void sort(int[] a){
        for (int i=0;i<a.length;i++){
            int temp = a[i];
            int low = 0;
            int high = i - 1;
            int mid = 0;
            while (high>=low){
                mid = (low + high) / 2;
                if (a[mid] > temp){
                    high = mid - 1;
                }else {
                    low = mid + 1;
                }
            }
            for (int j = i-1; j>=low; j--){
                a[j+1] = a[j];
            }
            a[low] = temp;
        }
    }
}
